class ManagerBorrowsController < AdminController
  def index
    @borrows = Borrow.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @borrows }
    end
  end

  def destroy
    @borrow = Borrow.find(params[:id])
    @borrow.destroy

    respond_to do |format|
      format.html { redirect_to manager_borrows_url }
      format.json { head :no_content }
    end
  end

  def delete_multiple
    if(params[:borrow_ids] != nil)
      @borrows = Borrow.find(params[:borrow_ids])
      @borrows.each do |borrow|
        borrow.destroy
      end
    end

    respond_to do |format|
      format.html { redirect_to manager_borrows_url }
      format.json { head :no_content }
    end
  end

  def search
    @borrows_users = nil    
    @users = User.find(:all, :conditions => ["idnumber LIKE ? AND email LIKE ?", "%" + params[:search_idnumber] + "%", "%" + params[:search_email] + "%"])
    @users.each do |user|
      if(@borrows_users != nil)
        @borrows_users = @borrows_users + user.borrows
      else
        @borrows_users = user.borrows
      end
    end
    
    @borrows_books = nil
    @books = Book.find(:all, :conditions => ["code LIKE ? AND title LIKE ?", "%" + params[:search_code] + "%", "%" + params[:search_title] + "%"])
    @books.each do |book|
      if(@borrows_books != nil)
        @borrows_books = @borrows_books + book.borrows
      else
        @borrows_books = book.borrows
      end
    end
    
    @borrows = @borrows_users & @borrows_books

    respond_to do |format|
      format.html
      format.json { head :no_content }
    end
  end
end
